/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.scribeplugin;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Calendar;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.pearreview.ui.*;
import com.pearreview.ui.clock.ScribeClock;
import com.pearreview.ui.datepicker.DatePicker;
import com.pearreview.application.Messages;
import com.pearreview.util.review.Meeting;
import com.pearreview.util.review.Protocol;

public class InformationPanel extends PearPanel implements ScribePanel {
	
	private static final long serialVersionUID = -3343346836303155821L;
	
	private FormLabel meetingDateLabel;
	private DateSpinner meetingDate;
	private FormLabel meetingStartLabel;
	private TimeSpinner meetingStart;
	private FormLabel meetingLocationLabel;
	private CompletableJTextField meetingLocation;
	private FormLabel meetingCommentsLabel;
	private JScrollPane scrollPaneMeetingComments;
	private FadingTextArea meetingComments;
	private JButton datePickerBtn;
		
	private JPanel formPanel;
	private JPanel newFormPanel;
	private GroupLayout layout;
	private HeadingLabel headingLabel;
	private BottomButtonPanel bottom;
	
	private JLabel slideList = new JLabel();
	
	public InformationPanel() {
		initComponents();
	}
	
	private void initComponents() {
		meetingDateLabel = new FormLabel(
				getLocalizedString("date"));
		meetingDate = new DateSpinner();
		meetingStartLabel = new FormLabel(
				getLocalizedString("start"));
		meetingStart = new TimeSpinner();
		meetingLocationLabel = new FormLabel(
				getLocalizedString("place"));
		meetingLocation = new CompletableJTextField();
		meetingCommentsLabel = new FormLabel(
				getLocalizedString("comments"));
		scrollPaneMeetingComments = new JScrollPane();
		meetingComments = new FadingTextArea();
		datePickerBtn = new JButton();
		//	Messages.getString("PearReview.Moderator.meetings.choose"));
			
			
		ActionListener dateAction = new ActionListener() { 
			@Override
			public void actionPerformed(ActionEvent e) {
				JPopupMenu datePicker = new DatePicker(meetingDate);
				datePicker.show(datePickerBtn, 0, datePickerBtn.getHeight());
				//datePicker.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
			}
	    };
	    
	    datePickerBtn.addActionListener(dateAction);		
		
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(680,400));
		
		setBackground(new Color(214, 214, 214));
			

		//======== scrollPaneReviewComments ========
		scrollPaneMeetingComments.setViewportView(meetingComments);
		
		//======== datePickerBtn ========
		ImageIcon ico = getResourceIcon("date.png");
		datePickerBtn.setIcon(ico);
		datePickerBtn.setToolTipText(
				getLocalizedString("choose"));
		datePickerBtn.setMargin(new Insets(0,0,0,0));
		
		newFormPanel = new JPanel();
		
		meetingComments.setEnabled(true);		
		
		//======== newMeetingPanel ========
		
		newFormPanel = new JPanel();
		newFormPanel.setOpaque(false);
		GroupLayout newLayout = new GroupLayout(newFormPanel);
		
		newFormPanel.setLayout(newLayout);
		newLayout.setHorizontalGroup(
				newLayout.createParallelGroup()
				.addGroup(newLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(newLayout.createParallelGroup()
						.addGroup(newLayout.createSequentialGroup()
							.addGroup(newLayout.createParallelGroup()
								.addGroup(
										GroupLayout.Alignment.TRAILING, 
										newLayout.createSequentialGroup()
									.addGroup(
											newLayout.createParallelGroup()
									.addComponent(meetingDateLabel, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											80, 
											GroupLayout.PREFERRED_SIZE)
									.addComponent(meetingStartLabel, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											80, 
											GroupLayout.PREFERRED_SIZE)
									.addComponent(meetingLocationLabel, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											80, 
											GroupLayout.PREFERRED_SIZE)
									.addComponent(meetingCommentsLabel, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											80, 
											GroupLayout.PREFERRED_SIZE))
								.addGap(57, 57, 57)
								.addGroup(newLayout.createParallelGroup(
										GroupLayout.Alignment.TRAILING)
									.addComponent(
											scrollPaneMeetingComments, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											300, 
											Short.MAX_VALUE)
									.addComponent(
											meetingLocation, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											300, 
											Short.MAX_VALUE)
									.addComponent(meetingStart, 
											GroupLayout.Alignment.LEADING, 
											GroupLayout.DEFAULT_SIZE, 
											300, 
											Short.MAX_VALUE)
									.addGroup(newLayout
											.createSequentialGroup()
										.addComponent(meetingDate, 
												GroupLayout.PREFERRED_SIZE, 
												265, 
												Short.MAX_VALUE)
										.addGap(10, 10, 10)
										.addComponent(datePickerBtn, 
												GroupLayout.PREFERRED_SIZE, 
												25, 
												GroupLayout.PREFERRED_SIZE)
		))))))));
		
		
		newLayout.setVerticalGroup(
				newLayout.createParallelGroup()
				.addGroup(
						newLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(newLayout.createParallelGroup(
							GroupLayout.Alignment.BASELINE)
						.addComponent(meetingDateLabel)
						.addComponent(meetingDate, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(datePickerBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(
							LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(newLayout.createParallelGroup()
						.addComponent(meetingStartLabel)
						.addComponent(meetingStart, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(
							LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(newLayout.createParallelGroup()
						.addComponent(meetingLocationLabel)
						.addComponent(meetingLocation, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(
							LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(newLayout.createParallelGroup()
						.addComponent(meetingCommentsLabel)
						.addComponent(scrollPaneMeetingComments, 
								GroupLayout.PREFERRED_SIZE, 
								90, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(
							LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(newLayout.createParallelGroup()))
		);
		
		
		
		
		//======== formPanel ========
		
		formPanel = new JPanel();
		formPanel.setOpaque(false);

		layout = new GroupLayout(formPanel);
		
		initFormPanel(newFormPanel);
		
		formPanel.setPreferredSize(new Dimension(300, 300));
		add(formPanel, BorderLayout.CENTER);
		headingLabel = new HeadingLabel(
				Messages.getString(
						"PearReview.Moderator.tabs.information"));
		add(headingLabel, BorderLayout.NORTH);
		
		bottom = new BottomButtonPanel();
		bottom.setFontSliderEnabled(true);
		bottom.addFontSliderChangeListener(new FontChangeListener());
		add(bottom, BorderLayout.SOUTH);
	}
		
	private void initFormPanel(JPanel currentFormPanel) {
		formPanel.removeAll();
		formPanel.setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					//.addContainerGap()
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addGroup(layout.createParallelGroup()
								.addGroup(GroupLayout.Alignment.TRAILING, 
										layout.createSequentialGroup()
									.addGroup(layout.createParallelGroup()
										.addComponent(currentFormPanel))
									.addGap(37, 37, 37)
									.addGroup(
											layout.createParallelGroup(
											GroupLayout.Alignment.TRAILING)
											.addComponent(slideList, 
												GroupLayout.PREFERRED_SIZE, 
												GroupLayout.DEFAULT_SIZE, 
												Short.MAX_VALUE))
									.addGap(10, 10, 10))))))
								
		);
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addComponent(currentFormPanel))
					.addGroup(layout.createSequentialGroup()
							.addContainerGap()
							.addComponent(slideList)
							.addContainerGap())
		);
	}
	
	/**
	 * Returns localized string of this component
	 * @param key key of string to localize
	 * @return localized string of this component
	 */
	private static String getLocalizedString(String key) {
		return Messages.getString("PearReview.Scribe.information." + key);
	}

	@Override
	public void setClock(ScribeClock clock) {
		this.headingLabel.setClock(clock);
	}
		
	/**
	 * Returns a resource icon
	 * @param name name of icon
	 * @return
	 */
	private ImageIcon getResourceIcon(String name) {
		return new ImageIcon(getClass().getResource("images/" + name));
	}

	public void setMeeting(Meeting meeting) {
		meetingDate.setDate(meeting.getPlannedDate().getTime());
		
		meetingStart.setDate(meeting.getPlannedStart().getTime());
		
		meetingLocation.setText(meeting.getPlannedLocation());
		
		meetingComments.setText(meeting.getComments());
	}
	
	public String getMeetingComments() {
		return meetingComments.getText();
	}
	
	public void setMeetingComments(String comments) {
		meetingComments.setText(comments);
	}
	
	public void setProtocol(Protocol protocol) {
		this.meetingDate.setDate(protocol.getDate().getTime());
		
		this.meetingStart.setDate(protocol.getStart().getTime());
		
		//this.meetingEnd.setDate(protocol.getEnd().getTime());
		
		this.meetingLocation.setText(protocol.getLocation());
		
		//this.meetingComments.setText(protocol.getComments());
	}
	
	public Protocol getProtocol() {
		return getProtocol(null);
	}
		
	public Protocol getProtocol(Protocol protocol) {
		if(protocol == null)
			protocol = new Protocol();
		
		Calendar cal = Calendar.getInstance();
		
		cal.clear();
		cal.setTime(meetingDate.getDate());
		
		protocol.setDate(cal);
		
		Calendar cal2 = Calendar.getInstance();

		cal2.clear();
		cal2.setTime(meetingStart.getDate());
		
		protocol.setStart(cal2);
		
		protocol.setLocation(meetingLocation.getText());
		
		return protocol;
	}

	@Override
	public String getHint() {
		return Messages.getString("PearReview.hint.scribe.information");
	}

	@Override
	public boolean isValidForm() {
		return true;
	}

	@Override
	public String getErrorMessage() {
		return null;
	}

	@Override
	public void setNextViewAction(Action action) {
		bottom.setNextViewAction(action);
	}

	@Override
	public void setPreviousViewAction(Action action) {
		bottom.setPreviousViewAction(action);
	}

	@Override
	public int getFontSize() {
		return bottom.getFontSize();
	}
	@Override
	public void setFontSize(int fontSize) {
		if(fontSize >= 0)
			bottom.setFontSize(fontSize);
	}
	
	private class FontChangeListener implements ChangeListener, Serializable {
		private static final long serialVersionUID = -1971767478503395523L;
		@Override
		public void stateChanged(ChangeEvent e) {
			updateFontSize();
		}
	}
	
	private void updateFontSize() {
		if(bottom != null) {
			int size = bottom.getFontSize();
			setFontToComponents(this, size);
		}
	}
	
	private void setFontToComponents(Component comp, int fontSize) {
		if(comp == bottom 
					|| comp == headingLabel 
					|| comp instanceof JLabel
					|| comp instanceof JButton)
			return;
			
		Font font = new Font(comp.getFont().getFontName(), 
				comp.getFont().getStyle(),
				fontSize);
		comp.setFont(font);
		
		if(comp instanceof JComboBox) {
			return;
		}
		
		if(comp instanceof Container) {
			Container cont = (Container) comp;
			for(int i = 0 ; i < cont.getComponentCount(); i++) {
				setFontToComponents(cont.getComponent(i), fontSize);
			}
		}
	}
}
